package com.qfedu.yanglao20admin.advertising.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.advertising.AdvertisingType;
import com.qfedu.yanglao20admin.advertising.dao.AdvertisingTypeDao;
import com.qfedu.yanglao20admin.advertising.param.AdvertisingTypeParam;
import com.qfedu.yanglao20admin.advertising.service.AdvertisingTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 广告类型信息业务层实现类
 */
@Service
public class AdvertisingTypeServiceImpl implements AdvertisingTypeService {

    @Resource
    private AdvertisingTypeDao advertisingTypeDao;

    @Override
    public AdvertisingType queryAdvertisingType(Integer id) {
        return advertisingTypeDao.selectById(id);
    }

    @Override
    public Page<AdvertisingType> pageAdvertisingType(AdvertisingTypeParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        //分页查询
        Page<AdvertisingType> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<AdvertisingType> advertisingTypes = advertisingTypeDao.selectList(param);

        //页码
        page.setCurrent(param.getPageNum());
        //每页查询的记录数
        page.setSize(param.getPageSize());
        //总记录数
        Long aLong = advertisingTypeDao.selectCount(null);
        page.setTotal(aLong);
        page.setRecords(advertisingTypes);
        return page;
    }

    @Override
    public AdvertisingType selectByName(String typeName) {

        return advertisingTypeDao.selectByName(typeName);
    }

    @Override
    public void add(AdvertisingType advertisingType) {
        if (advertisingType.getTypeName() == null || advertisingType.getTypeName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写广告类型");
        }

        AdvertisingType advertisingType1 = advertisingTypeDao.selectByName(advertisingType.getTypeName());
        if (advertisingType1 != null) {
            throw new CommonException(CommonConstants.ERROR, "该广告类型已存在");
        }
        advertisingTypeDao.insert(advertisingType);
    }

    @Override
    public void update(AdvertisingType advertisingType) {
        if (advertisingType.getTypeName() == null || advertisingType.getTypeName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写广告类型");
        }

        AdvertisingType advertisingType1 = advertisingTypeDao.selectByName(advertisingType.getTypeName());
        if (advertisingType1 != null) {
            if (!advertisingType1.getId().equals(advertisingType.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该摄像头类型已存在");
            }
        }
        advertisingTypeDao.updateById(advertisingType);
    }

    @Override
    public void deleteById(Integer id) {
        advertisingTypeDao.deleteById(id);
    }
}
